package com.chuangyang.fixboxlib.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuangyang.fixboxlib.R;

/**
 * Created by vanshu on 15/4/8 13:07.
 */
public class ImageButton extends LinearLayout {
    private ImageView listItemIcon;
    private TextView listItemDescription;

    public ImageButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
        drawView(context, attrs);

    }

    private void init() {
        setClickable(true);
        setFocusable(true);
    }


    public void setImageText(String text) {
        listItemDescription.setText(text);
    }

    public  void setImageTextColor(int color){
        listItemDescription.setTextColor(color);
    }

    private void drawView(Context context, AttributeSet attrs) {
        TypedArray a = null;
        View view;
        try {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.image_button_layout, this, true);
            listItemIcon = (ImageView) view.findViewById(R.id.list_item_icon);
            listItemDescription = (TextView) view.findViewById(R.id.list_item_des);
            a = context.obtainStyledAttributes(attrs, R.styleable.ImageButton);
            Drawable src = a.getDrawable(R.styleable.ImageButton_imageSrc);
            String description = a.getString(R.styleable.ImageButton_android_text);
            int textColor = a.getInt(R.styleable.ImageButton_android_textColor, -1);
            if (src != null) {
                listItemIcon.setImageDrawable(src);
            }
            if (description != null) {
                listItemDescription.setText(description);
                listItemDescription.setTextColor(textColor);
            }

        } catch (Exception e) {
        } finally {
            if (a != null)
                a.recycle();
        }
    }
}
